import Vue from 'vue';
import Router from 'vue-router';

Vue.use(Router);

const VueRouter = new  Router({
    // mode: 'history',
    routes: [
        {
            path: '/login',
            component: () => import("../view/login/login"),
            meta: { title: '登录'}
        },
        {
            path: '/',
            redirect: '/mainPool'
        },
        {
            path: '/',
            component: () => import('../view/home/home'),
            meta: {title: '系统',requireAuth: true},
            children: [
                {
                    path: '/mainPool',
                    component: () => import( '../view/home/mainPool'),
                    meta: { title: '首页',requireAuth: true}
                },
                {
                    path: '/reservePart',
                    component: () => import( '../view/spare/reservePart'),
                    meta: { title: '一次设备备品',requireAuth: true}
                },
                {
                    path: '/equipmentSpare',
                    component: () => import('../view/spare/equipmentSpare'),
                    meta: { title: '二次设备备品',requireAuth: true}
                },
                {
                    path: '/newWarehouse',
                    component: () => import('../view/spare/newWarehouse'),
                    meta: { title: '仓库新增',requireAuth: true}
                },
                {
                    path: '/secondaryPart',
                    component: () => import('../view/spare/secondaryPart'),
                    meta: { title: '二次设备备件',requireAuth: true}
                },
                {
                    path: '/replacementPart',
                    component: () => import('../view/spare/replacementPart'),
                    meta: { title: '一次设备备件',requireAuth: true}
                },
                {
                    path: '/teamStandingBook',
                    component: () => import('../view/check/teamStandingBook'),
                    meta: { title: '备品班组审核',requireAuth: true}
                },
                {
                    path: '/departmentStandingBook',
                    component: () => import('../view/check/departmentStandingBook'),
                    meta: { title: '备品部门审核',requireAuth: true}
                },
                {
                    path: '/teamSparePart',
                    component: () => import('../view/check/teamSparePart'),
                    meta: { title: '备件班组审核',requireAuth: true}
                },
                {
                    path: '/departmentSparePart',
                    component: () => import('../view/check/departmentSparePart'),
                    meta: { title: '备件部门审核',requireAuth: true}
                },
                {
                    path: '/teamWarehouse',
                    component: () => import('../view/check/teamWarehouse'),
                    meta: { title: '仓库班组审核',requireAuth: true}
                },
                {
                    path: '/departmentWarehouse',
                    component: () => import('../view/check/departmentWarehouse'),
                    meta: { title: '仓库部门审核',requireAuth: true}
                },
                {
                    path: '/storageSpareParts',
                    component: () => import('../view/stockManagement/storageSpareParts'),
                    meta: { title: '备品备件入库',requireAuth: true}
                },
                {
                    path: '/recipientsApply',
                    component: () => import('../view/recipientsManagement/recipientsApply'),
                    meta: { title: '领用申请',requireAuth: true}
                },
                {
                    path: '/recipientsConfirm',
                    component: () => import('../view/recipientsManagement/recipientsConfirm'),
                    meta: { title: '领用确认',requireAuth: true}
                },
                {
                    path: '/dailyOperation',
                    component: () => import('../view/purchasingManagement/dailyOperation'),
                    meta: { title: '日常运维采购',requireAuth: true}
                },
                {
                    path: '/fixedProcurement',
                    component: () => import('../view/purchasingManagement/fixedProcurement'),
                    meta: { title: '定额采购',requireAuth: true}
                },
                {
                    path: '/procurementPlan',
                    component: () => import('../view/purchasingManagement/procurementPlan'),
                    meta: { title: '采购计划',requireAuth: true}
                },
                {
                    path: '/inventoryRegistered',
                    component: () => import('../view/inventoryManagement/inventoryRegistered'),
                    meta: { title: '盘点登记',requireAuth: true}
                },
                {
                    path: '/recordInquiry',
                    component: () => import('../view/inventoryManagement/recordInquiry'),
                    meta: { title: '盘点记录查询',requireAuth: true}
                },
                {
                    path: '/belowQuotaReminder',
                    component: () => import('../view/quotaManagement/belowQuotaReminder'),
                    meta: { title: '低于定额提醒',requireAuth: true}
                },
                {
                    path: '/reserveQuotaSetting',
                    component: () => import('../view/quotaManagement/reserveQuotaSetting'),
                    meta: { title: '储备定额设置',requireAuth: true}
                },
                {
                    path: '/retirement',
                    component: () => import('../view/retirementManagement/retirement'),
                    meta: { title: '报废',requireAuth: true}
                },
                // 试验
                {
                    path: '/beyondTime',
                    component: () => import('../view/testingManagement/beyondTime'),
                    meta: { title: '超期未试验',requireAuth: true}
                },{
                    path: '/closeTest',
                    component: () => import('../view/testingManagement/closeTest'),
                    meta: { title: '临近试验',requireAuth: true}
                },{
                    path: '/compilation',
                    component: () => import('../view/testingManagement/compilation'),
                    meta: { title: '任务单编制',requireAuth: true}
                },{
                    path: '/cycleSet',
                    component: () => import('../view/testingManagement/cycleSet'),
                    meta: { title: '周期设置',requireAuth: true}
                },{
                    path: '/execution',
                    component: () => import('../view/testingManagement/execution'),
                    meta: { title: '任务单执行',requireAuth: true}
                },{
                    path: '/replacementTestItems',
                    component: () => import('../view/testingManagement/replacementTestItems'),
                    meta: { title: '备件试验项目',requireAuth: true}
                },{
                    path: '/spareTestItems',
                    component: () => import('../view/testingManagement/spareTestItems'),
                    meta: { title: '备品试验项目',requireAuth: true}
                }
            ]
        },
        {
            path: '/permission',
            component: () => import('../view/permission/permission'),
            meta: { title: '权限测试',requireAuth: true}
        },
        {
            path: '/403',
            component: () => import('../view/permission/403'),
            meta: { title: '403',requireAuth: true}
        },
        {
            path: '*',
            component:  () => import('../view/permission/404'),
            meta: { title: '404',requireAuth: true}
        }
    ]
})

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}
//使用钩子函数对路由进行权限跳转
VueRouter.beforeEach((to, from, next) => {
    const info = JSON.parse(sessionStorage.getItem('information')) || null
    const token = info ? info.token : null
    if(to.path == "/login"){
        token ? next("/") : next();
    }else{
        // requireAuth:可以在路由元信息指定哪些页面需要登录权限
        if(to.meta.requireAuth && token) {
            next();
        }else{
            next("/login");
        }
    }
});
export default VueRouter